package map.android.baidu.carpool.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeUtil {
    private static final long DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;

    /**
     * 根据年 月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int year, int month) {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据日期 找到对应日期的 星期
     */
    public static String getDayOfWeekByDate(String date) {
        String dayOfweek = "-1";
        try {
            SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
            Date myDate = myFormatter.parse(date);
            SimpleDateFormat formatter = new SimpleDateFormat("E");
            String str = formatter.format(myDate);
            dayOfweek = str;

        } catch (Exception e) {
            System.out.println("错误!");
        }
        return dayOfweek;
    }

    public static int getCurrentYear() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR);
    }

    public static int getCurrentMonth() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getCurrentDay() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DATE);
    }

    public static boolean isToday(int year, int month, int day) {
        if (year == getCurrentYear() && month == getCurrentMonth() && day == getCurrentDay()) {
            return true;
        }
        return false;
    }

    public static boolean isTomorrow(int year, int month, int day) {
        if (year == getNextDayYear() && month == getNextDayMonth() && day == getNextDayDay()) {
            return true;
        }
        return false;
    }

    public static boolean isYesterday(int year, int month, int day) {
        if (year == getYesterdayYear() && month == getYesterdayMonth() && day == getYesterdayDay()) {
            return true;
        }
        return false;
    }

    public static boolean isDayAfterTomorrow(int year, int month, int day) {
        if (year == getDayAfterTomorrowYear() && month == getDayAfterTomorrowMonth()
                && day == getDayAfterTomorrowDay()) {
            return true;
        }
        return false;
    }

    public static int getCurrentYearByCertainDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        return c.get(Calendar.YEAR);
    }

    public static int getCurrentMonthByCertainDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getCurrentDayByCertainDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        return c.get(Calendar.DATE);
    }

    public static int getNextDayYear() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, +1);
        return c.get(Calendar.YEAR);
    }

    public static int getNextDayMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, +1);
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getNextDayDay() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, +1);
        return c.get(Calendar.DATE);
    }

    public static int getYesterdayYear() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        return c.get(Calendar.YEAR);
    }

    public static int getYesterdayMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getYesterdayDay() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        return c.get(Calendar.DATE);
    }

    public static Calendar getYesterdayInstance() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        return c;
    }

    public static int getDayAfterTomorrowYear() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, +2);
        return c.get(Calendar.YEAR);
    }

    public static int getDayAfterTomorrowMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, +2);
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getDayAfterTomorrowDay() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, +2);
        return c.get(Calendar.DATE);
    }

    public static int getYearByThreeMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, +3);
        return c.get(Calendar.YEAR);
    }

    public static int getMonthByThreeMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, +3);
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getDayByThreeMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, +3);
        return c.get(Calendar.DATE);
    }

    public static int getNextDayYearByCertainDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        c.add(Calendar.DATE, +1);
        return c.get(Calendar.YEAR);
    }

    public static int getNextDayMonthByCertainDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        c.add(Calendar.DATE, +1);
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getNextDayDayByCertainDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        c.add(Calendar.DATE, +1);
        return c.get(Calendar.DATE);
    }

    public static int getYearByThreeMonthAndCertainDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        c.add(Calendar.MONTH, +3);
        return c.get(Calendar.YEAR);
    }

    public static int getMonthByThreeMonthAndCertainDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        c.add(Calendar.MONTH, +3);
        return c.get(Calendar.MONTH) + 1;
    }

    public static int getDayByThreeMonthAndCertainDay(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day);
        c.add(Calendar.MONTH, +3);
        return c.get(Calendar.DATE);
    }

    public static Date getDateAfterTodayNday(Date today, int num) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(today);
        calendar.add(calendar.DATE, num);

        return calendar.getTime();
    }

    public static Date getMonthAfterNMonth(Date today, int num) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(today);
        calendar.add(calendar.MONTH, num);
        return calendar.getTime();
    }

    public static boolean dateIsEqual(Date date1, Date date2) {
        return date1.getYear() == date2.getYear()
                && date1.getMonth() == date2.getMonth()
                && date1.getDate() == date2.getDate();

    }

    public static boolean isToday(Calendar calendar) {
        Calendar mCalendarNow = Calendar.getInstance();
        return mCalendarNow.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)
                && mCalendarNow.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)
                && mCalendarNow.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static boolean isTomorrow(Calendar calendar) {
        Calendar mCalendarNow = Calendar.getInstance();
        return calendar.after(mCalendarNow)
                && calendar.get(Calendar.DAY_OF_MONTH) - mCalendarNow.get(Calendar.DAY_OF_MONTH) == 1 && calendar.get
                (Calendar.YEAR) == mCalendarNow.get(Calendar.YEAR) && calendar.get
                (Calendar.MONTH) == mCalendarNow.get(Calendar.MONTH);
    }

    public static boolean isAfter(Calendar calendar) {
        Calendar mCalendarNow = Calendar.getInstance();
        return calendar.after(mCalendarNow)
                && calendar.get(Calendar.DAY_OF_MONTH) - mCalendarNow.get(Calendar.DAY_OF_MONTH) == 2 && calendar
                .get(Calendar.YEAR) == mCalendarNow.get(Calendar.YEAR) && calendar.get
                (Calendar.MONTH) == mCalendarNow.get(Calendar.MONTH);

    }
}
